package com.qf.air.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.air.entity.Air;
import com.qf.air.enums.AirEnum;
import com.qf.air.exception.AirEx;
import com.qf.air.mapper.AirMapper;
import com.qf.air.mapper.DistrictMapper;
import com.qf.air.service.AirService;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 维多利加
 * @Date: 2021/3/20 11:02
 */
@Service
public class AirServiceImpl implements AirService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private AirMapper airMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Override
    public PageInfo<Air> findByPageAndDistrictId(Integer page, Integer rows, Integer districtId, String monitoringStation, Date startTime, Date endTime) {
        PageHelper.startPage(page,rows);
        List<Air> all = airMapper.findByDistrictId(districtId,monitoringStation,startTime,endTime);
        PageInfo<Air> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(Air air) {
        Integer count = airMapper.save(air);
        if(count !=1){
            //添加失败，记录日志，抛出异常
            System.out.println("添加信息失败！ air="+air);
            throw new AirEx(AirEnum.AIR_ADD_ERROR);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout = -1,readOnly = false,rollbackFor = Exception.class,noRollbackFor = ArithmeticException.class)
    public void deleteById(Integer id) {
        int i = airMapper.deleteById(id);

        if(i!=1){
            System.out.println("删除失败！ id="+id);
            throw new AirEx(AirEnum.AIR_DELETE_ERROR);
        }

    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateById(Air air) {
        int count = airMapper.updateById(air);

        if(count!=1){
            System.out.println("修改失败！ air="+air);
            throw new AirEx(AirEnum.AIR_UPDATE_ERROR);
        }
    }

    @Override
    public void saveEs(Air air) {
        //创建request
        IndexRequest request = new IndexRequest();
        request.index("air_index");
        request.id(air.getId().toString());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Map source = new HashMap();
        source.put("id", air.getId());
        source.put("districtId", air.getDistrictId());
        source.put("districtName", districtMapper.findById(air.getDistrictId()));
        source.put("monitorTime", simpleDateFormat.format(air.getMonitorTime()));
        source.put("pm10", air.getPm10());
        source.put("pm25", air.getPm25());
        source.put("monitoringStation", air.getMonitoringStation());
        source.put("lastModifyTim", simpleDateFormat.format(new Date()));

        request.source(source);
        IndexResponse response = null;
        try {
            response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(response.getResult());
    }

    @Override
    public void deleteByIdEs(Integer id) {

    }

    @Override
    public void updateByIdEs(Air air) {

    }
}
